// 存储用户信息
import { UserInfo } from '@/types/profile'
import { makeAutoObservable } from 'mobx'
import { makePersistable } from 'mobx-persist-store'

class Profile {
  constructor() {
    makeAutoObservable(this)
    // 数据持久化
    makePersistable(this, {
      name: 'profile',
      properties: ['token', 'userInfo'],
      storage: window.localStorage
    })
  }

  /** 用户 token */
  token = ''
  /** 用户信息 */
  userInfo: UserInfo = {
    birthday: '',
    gender: 0,
    id: '',
    intro: '',
    mobile: '',
    name: '',
    photo: ''
  }

  /** 存储用户 token */
  saveToken = (token: string) => {
    this.token = token
  }
  /** 存储用户信息 */
  saveUserInfo = (userInfo: UserInfo) => {
    this.userInfo = userInfo
  }
  /** 清空信息 */
  clearUserInfo = () => {
    ;(this.token = ''),
      (this.userInfo = {
        birthday: '',
        gender: 0,
        id: '',
        intro: '',
        mobile: '',
        name: '',
        photo: ''
      })
  }
}

export const useProfile = new Profile()
